package dtd.PHS.VND_ExchangeRate.ui.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.R.id;
import dtd.PHS.VND_ExchangeRate.R.layout;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public class RatesAdapter extends BaseAdapter {

	private ListRates listRates;

	public RatesAdapter(Context context,ListRates listRates) {
		super();
		this.listRates = listRates;
	}

	@Override
	public int getCount() {
		return listRates.size();
	}

	@Override
	public Object getItem(int pos) {
		return listRates.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	public class Holder {

		public ImageView ivFlag;
		public TextView tvBuying;
		public TextView tvTransfer;
		public TextView tvSelling;

	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		View v = convertView;
		Holder holder = new Holder();
		if ( v == null) {
			v = Helpers.inflateView(R.layout.rate_item, null);
			holder.ivFlag = (ImageView) v.findViewById(R.id.ivFlag);
			holder.tvBuying = (TextView) v.findViewById(R.id.tvBuyingRate);
			holder.tvTransfer = (TextView) v.findViewById(R.id.tvTransferRate);
			holder.tvSelling = (TextView) v.findViewById(R.id.tvSellingRate);
			v.setTag(holder);
		} else holder = (Holder) v.getTag();
		
		holder.ivFlag.setImageResource(Helpers.getFlagIconName(listRates.get(position).getCurrencyName()));
		holder.tvBuying.setText(Helpers.formatRate(listRates.get(position).getBuying()));
		holder.tvTransfer.setText(Helpers.formatRate(listRates.get(position).getTransfer()));
		holder.tvSelling.setText(Helpers.formatRate(listRates.get(position).getSelling()));
		
		return v;
	}

}
